Welcome to your essential 2025 guide to the Terraform local-exec provisioner! Do you need to run a command or execute a script on the same machine where you are running terraform apply or terraform destroy, triggered by the creation or destruction of a resource? The local-exec provisioner is designed for precisely this scenario, allowing you to invoke local processes as part of your Terraform workflow.
This comprehensive tutorial, useful for any Terraform user regardless of the cloud provider (AWS, Azure, GCP), will provide a clear, step-by-step walkthrough of how to use this provisioner. We will explain its core purpose – executing commands locally, not on the remote resource itself – and demonstrate its syntax (provisioner "local-exec" {}). You will learn about its key arguments:
command: The command string to execute on your local machine. This command can use attributes from the resource it's attached to (e.g., passing an IP address to a local script).
interpreter: Specifies the interpreter to use for the command (e.g., ["/bin/bash", "-c"] for Bash scripts).
working_dir: The directory on your local machine from which to run the command.
when: Controls whether the provisioner runs during resource creation (create - the default) or destruction (destroy).
We will provide practical examples, demonstrating how local-exec could be used to:
Run a local script (./configure-dns.sh) after an aws_instance is created, passing the instance's public IP address as an argument.
T
|
When you're negotiating your salary for ...
What are recent advances in the field of...
Today Quincy Larson interviews Kunal Kus...
Arrow functions don't have their own 'th...
Learn Git and GitHub from scratch with c...
freeCodeCamp runs right in the browser -...
This is part two of our two episode seri...
See how Gemini 3 writes code and builds ...
Download your free Python Cheat Sheet he...